package com.example.json;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;

public class MainActivity extends AppCompatActivity {
private static final String TAG="tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }
    private void initview(){
        Button button=findViewById(R.id.btn_json_post);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendPost();
            }
        });
    }
    private void sendPost(){
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL url=new URL("http://apis.juhe.cn/simpleWeather/query?city="+ URLEncoder.encode("西安","UTF-8"));
                 HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                   connection.setConnectTimeout(10*1000);
                   connection.setReadTimeout(10*1000);
                   connection.setRequestMethod("POST");
                   connection.setUseCaches(false);
                   connection.setDoOutput(true);
                   String param="&key="+URLEncoder.encode("f316e87349acc334385260de4a67b6bc","UTF-8");
                    OutputStream out=connection.getOutputStream();
                    out.write(param.getBytes());
                    out.flush();
                    if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                              String realtime= readPostInputstream(connection.getInputStream());
                          FutureBean[] bean=JsonUtil.parseBeanLiat(realtime);
                            for (FutureBean f:bean){
                                Log.d(TAG, "date= " + f.getDate() + "temperature=" + f.getTemperature() + "weather=" + f.getWeather()
                                        +"direct="+f.getDirect()+"wid="+f.getWid());
                               // Log.d(TAG, "city= "+f.getCity()+" realtime= "+f.getRealtime()+" future= "+f.getFuture());
                            }




                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    private String readPostInputstream(InputStream is) throws IOException {
        InputStreamReader reader=new InputStreamReader(is);
        BufferedReader buff=new BufferedReader(reader);
        StringBuffer sb=new StringBuffer();
        String str="";
        while (null!=(str=buff.readLine())){
            sb.append(str);
        }
       return sb.toString();
    }
}